/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_redirect_unpack.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/21 by geyh                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "../../../../src/module/oracle/config.h"
#include "../../test_ora_comm.h"
#include <oracle/comm/ora_comm.h>
#include <apmtest_comm.h>
#include <frservice_mng.h>
#include <frservice_comm.h>
#include <oracle/tns/ora_redirect.h>
#include <frct_comm.h>
#include <frct_expect.h>

#include "t_ora_redirect.h"

extern ApmContext g_st_apm_context;
extern void *g_p_apm_context;

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}


static void __case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L7_ora_packet *p_st_ora_packet = NULL;
    Frhash_header *p_st_hash_report = NULL;
    Apm_packet *p_st_packet = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;
    
    p_st_ora_packet = p_st_session_private->p_st_ora_packet;
    
    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x00\x42\x00\x00\x05\x00\x00\x00\x00\x38\x28\x41\x44\x44\x52\x45"
            "\x53\x53\x3d\x28\x50\x52\x4f\x54\x4f\x43\x4f\x4c\x3d\x74\x63\x70\x29\x28\x48\x4f\x53\x54\x3d\x31\x39\x32\x2e"
            "\x31\x36\x38\x2e\x31\x38\x2e\x31\x36\x37\x29\x28\x50\x4f\x52\x54\x3d\x32\x34\x38\x37\x29\x29", 66);

    frexpect_init(0);
    frexpect_setRef(g_st_apm_context.p_st_exp_ref);
    g_p_apm_context = &g_st_apm_context;

    i_ret = ora_redirect_unpack(p_st_conn, p_st_sess, p_st_ora_packet, p_st_hash_report, p_st_packet);
    
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    
    bytebuff_free(&p_st_ora_packet->p_st_packet);

    frexpect_destory();
    test_ora_destroy_ct(&p_st_conn);
    return;
    
}
//
//static void __case2(void)
//{
//    int i_ret = 0;
//    Conntrack *p_st_conn = NULL;
//    L7_ora_packet *p_st_ora_packet = NULL;
//    Frhash_header *p_st_hash_report = NULL;
//    Apm_packet *p_st_packet = NULL;
//
//    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
//    L7_session *p_st_session = frct_get_session(p_st_conn);
//    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
//    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;
//
//    p_st_ora_packet = p_st_session_private->p_st_ora_packet;
//
//    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x00", 1);
//
//    i_ret = ora_redirect_unpack(p_st_conn, p_st_sess, p_st_ora_packet, p_st_hash_report, p_st_packet);
//
//    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
//
//    bytebuff_free(&p_st_ora_packet->p_st_packet);
//
//    test_ora_destroy_ct(&p_st_conn);
//    return;
//
//}

int add_suite_ora_redirect_unpack(void)
{
    CU_pSuite pSuite = NULL;
    
    if ((pSuite = CU_add_suite(MODULE_NAME" ora_redirect_unpack()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;
    
    CU_add_test(pSuite, "case1", __case1);
//    CU_add_test(pSuite, "case2", __case2);
//    CU_add_test(pSuite, "case3", __case3);
//    CU_add_test(pSuite, "case4", __case4);
    
    return FR_SUCC;
}
